#include <bits/stdc++.h>
using namespace std;
int T;
int n;
int a[1000005];
vector <int> cnt;
long long ans;
int main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    int x;
    scanf("%d",&T);
    while(T--)
    {
        cnt.clear();
        ans=0;
        memset(a,0,sizeof(a));
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&x);
            if(!a[x]) cnt.push_back(x);
            a[x]++;
        }
        for(int i=0;i<cnt.size();i++)
        {
            cout<<cnt[i]<<" "<<a[cnt[i]]<<endl;
            ans+=(a[cnt[i]]-1)*cnt[i];
        }
        printf("%d\n",ans);
    }
    return 0;
}
